<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 13] 13.2 Colors</TITLE>
<META NAME="author" CONTENT="Pat Niemeyer and Josh Peck">
<META NAME="date" CONTENT="Tue Jul 22 19:02:58 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java">
<META NAME="title" CONTENT="Exploring Java">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Exploring Java" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch13_01.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 13<br>Drawing With the AWT</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch13_03.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="ch13-SECT1-AUTOID.1">13.2 Colors</A></h2>

<P CLASS=para>
The <tt CLASS=literal>TestPattern</tt> applet fills its shapes with a
number of colors, using the <tt CLASS=literal>setColor()</tt> method of the
<tt CLASS=literal>Graphics</tt> object. <tt CLASS=literal>setColor()</tt> sets
the current color in the graphics context, so we set it to a different
color before each drawing operation. But where do these color values
come from?

<P CLASS=para>
The <tt CLASS=literal>java.awt.Color</tt> class handles color in Java. A
<tt CLASS=literal>Color</tt> object describes a single color. You can
create an arbitrary <tt CLASS=literal>Color</tt> by specifying the red,
green, and blue values, either as integers between 0 and 255 or as
floating-point values between 0.0 and 1.0. You can also use
<tt CLASS=literal>getColor()</tt> to look up a named color in the system
properties table, as described in <A HREF="ch07_01.htm">Chapter 7, <i>Basic Utility Classes</i></A>. 
<tt CLASS=literal>getColor()</tt> takes a
<tt CLASS=literal>String</tt> color property name, retrieves the integer
value from the <tt CLASS=literal>Properties</tt> list, and returns the
<tt CLASS=literal>Color</tt> object that corresponds to that color.

<P CLASS=para>
The <tt CLASS=literal>Color</tt> class also defines a number of
<tt CLASS=literal>static final</tt> color values; these are what we used
in the <tt CLASS=literal>TestPattern</tt> example. These constants, such
as <tt CLASS=literal>Color.black</tt> and <tt CLASS=literal>Color.red</tt>,
provide a convenient set of basic colors for your drawings.

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="ch13-SECT2-AUTOID.1">Desktop Colors</A></h3>

<P CLASS=para>
The <tt CLASS=literal>Color</tt> class I just described makes it
easy to construct a particular color; however, that's not always what
you want to do. Sometimes you want to match a preexisting color
scheme. This is particularly important when you are designing a user
interface; you might want your components to have the same colors as
other components on that platform, and to change automatically if the
user redefines his or her color scheme. 

<P CLASS=para>
That's what the <tt CLASS=literal>SystemColor</tt> class is for. A system
color represents the color used by the local windowing system in a
certain context.  The SystemColor class holds lots of pre-defined
SystemColors, just like the Color Class holds some pre-defined basic
colors.  For example, the field <tt CLASS=literal>activeCaption</tt>
represents the color used for the background to the title of an active
window; <tt CLASS=literal>activeCaptionText</tt> represents the color used
for the title itself. <tt CLASS=literal>menu</tt> represents the
background color of menu selection; <tt CLASS=literal>menuText</tt>
represents the color of a menu item's text when it is not selected;
<tt CLASS=literal>textHighlightText</tt> is the color used when the item
is selected; and so on.  You could use the <tt CLASS=literal>window</tt>
value to set the color of a <tt CLASS=literal>Window</tt> to match the
other Windows on the user's screen--whether or not they're generated
by Java programs.

<DIV CLASS=programlisting>
<P>
<PRE>
myWindow.setBackground( SystemColor.window );
</PRE>
</DIV>

<P CLASS=para>
Because the <tt CLASS=literal>SystemColor</tt> class is a
subclass of <tt CLASS=literal>Color</tt>, you can use it
wherever you would use a <tt CLASS=literal>Color</tt>.
However, the <tt CLASS=literal>SystemColor</tt> constants are
tricky. They are constants as far as you, the programmer, are
concerned; your code is not allowed to modify them. However, they can
be modified at run-time by the Toolkit. If the user changes his color
scheme, the system colors are automatically updated to follow suit; as
a result, anything displayed with system colors will also change color
the next time it is redrawn. For example, the window
<tt CLASS=literal>myWindow</tt> would automatically change its
background color to the new background color. 

<P CLASS=para>
The <tt CLASS=literal>SystemColor</tt> class has one noticeable
shortcoming. You can't compare a system color to a
<tt CLASS=literal>Color</tt> directly; 
the <tt CLASS=literal>Color.equals()</tt> method doesn't return
reliable results. For example, if you want to find out whether the
window background color is red, you can't call:
<DIV CLASS=programlisting>
<P>
<PRE>
Color.red.equals(SystemColor.window);
</PRE>
</DIV>


Instead, you should use <tt CLASS=literal>getRGB()</tt> to find
the color components of both objects, and compare them, rather than
comparing the objects themselves.

</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch13_01.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch13_03.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Basic Drawing</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Fonts</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
